"""
@Author: Eobard Gu
@Date: 2025-07-31
@Description: 更改图片文件md5
    使用该脚本需要安装依赖
        pip3 install opencv-python
        pip3 install numpy
        pip3 install pillow
"""
import os
import cv2
import sys
import random
import numpy as np
from PIL import Image


def main():
    if len(sys.argv)<3:
        print("python脚本执行失败：图片源路径或图片保存路径不能为空")
        sys.exit(1)
    
    raw_path=sys.argv[1]
    target_path=sys.argv[2]

    if not raw_path.strip() or not target_path.strip():
        print("python脚本执行失败：图片源路径或图片保存路径不能为空")
        sys.exit(1)

    if not os.path.exists(target_path):
        os.makedirs(target_path)
    
    print("开始处理图片...")
    # 处理图片
    for image_name in os.listdir(raw_path):
        if image_name.endswith("jpg"):
            image_abs_path = os.path.join(raw_path, image_name)
            image_rgb = cv2.imread(image_abs_path)
            save_file_path = os.path.join(target_path, image_name)
            cv2.imwrite(save_file_path, image_rgb)

        if image_name.endswith("png"):
            image_abs_path = os.path.join(raw_path, image_name)
            
            pil_image = Image.open(image_abs_path).convert('RGBA')
            img_array = np.asarray(pil_image).copy()
            h, w, a = img_array.shape
            fix_num = random.randint(5, 10)
            for i in range(h):
                for j in range(w):
                    for k in range(a):
                        item = img_array[i][j][k]
                        if 10 < item < 90 and random.randint(0, 100) >= 50:
                            if random.randint(1, 5) >= 3:
                                img_array[i][j][k] = item - random.randint(1, 3)
                            else:
                                img_array[i][j][k] = item + random.randint(1, 3)
                            fix_num = fix_num - 1
                        if fix_num <= 0:
                            break
                    if fix_num <= 0:
                        break
                if fix_num <= 0:
                    break
            pil_image2 = Image.fromarray(np.uint8(img_array))
            save_file_path = os.path.join(target_path, image_name)
            pil_image2.save(save_file_path, 'PNG')
    print(f"{raw_path}:图片md5更改成功")


if __name__ == "__main__":
    main()  